package com.kunbo.cn.service.impl.core.supplier;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.supplier.SupplierUserDao;
import com.kunbo.cn.entity.core.supplier.SupplierUser;
import com.kunbo.cn.service.core.supplier.SupplierUserService;
import com.kunbo.cn.util.encrypt.MD5Utils;

@Service
public class SupplierUserImpl implements SupplierUserService {

	@Autowired
	private SupplierUserDao dao;
	@Autowired
	private JdbcTemplate jdbc;

	@Override
	public Boolean delSupplier(String id) {
		// TODO Auto-generated method stub
		dao.delete(id);
		SupplierUser supplier = dao.findOne(id);
		if (supplier != null)
			return true;
		else
			return false;
	}

	@Override
	public Boolean updPassword(String id, String password) {
		// TODO Auto-generated method stub
		SupplierUser supplier = dao.findOne(id);
		supplier.setLogin_password(MD5Utils.md5(password));
		supplier = dao.saveAndFlush(supplier);
		if (supplier != null)
			return true;
		else
			return false;
	}

	@Override
	public List<Map<String, Object>> selSupplierList(String code, String login_name, String supplier, Integer page,
			Integer pageSize) {
		// TODO Auto-generated method stub
		String sql = "select a.id,a.supplier_code,a.supplier,a.login_name,a.supplier_name,a.login_password,a.phone,a.affiliated_organization,a.type,a.tives,a.explains,a.login_count,a.last_login_time from business_supplier_users_manage a  where 1 =1";
		if (code != null && !code.equals("")) {
			sql += " and a.supplier_code like '%" +code+ "%'";
		}
		if (login_name != null && !login_name.equals("")) {
			sql += " and a.login_name like '%" + login_name +"%'";
		}
		if (supplier != null ) {
			sql += " and a.supplier_name like '%" + supplier + "%'";
		}
		sql += " order by id desc limit " + (page*pageSize) + "," + pageSize;
		System.out.println(sql);
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		if(list != null) {
			for (Map<String, Object> map : list) {
				String l = "select name from business_supplier_manage where id = " + Integer.parseInt(map.get("supplier").toString());
				List<Map<String, Object>> suppliers =jdbc.queryForList(l);
				if(suppliers != null) {
						map.put("supplier", suppliers.get(0).get("name").toString());
				}
				map.put("count", list.size());
			}
			
		}
		return jdbc.queryForList(sql);
	}

	@Override
	public Boolean updSupplier(String id, String code, Integer supplier, String loginName, String supplierName,
			String loginPassword, String phone, String organization, String type, Integer effective, String explain) {
		// TODO Auto-generated method stub
		SupplierUser suppliers = dao.findOne(id);
		suppliers.setSupplier_code(code);
		suppliers.setSupplier(supplier);
		suppliers.setLogin_name(loginName);
		suppliers.setSupplier_name(supplierName);
		suppliers.setLogin_password(MD5Utils.md5(loginPassword));
		suppliers.setPhone(phone);
		suppliers.setAffiliated_organization(organization);
		suppliers.setType(type);
		suppliers.setTives(effective);
		suppliers.setExplains(explain);
		suppliers = dao.saveAndFlush(suppliers);
		if (suppliers != null)
			return true;
		else
			return false;
	}

	@Override
	public SupplierUser saveSupplier(String code, Integer supplier, String loginName, String supplierName,
			String loginPassword, String phone, String organization, String type, Integer effective, String explain) {
		// TODO Auto-generated method stub
		String customerSql = "select login_name from business_customer_users_manage where login_name = '" + loginName + "'";
		String supplierSql = "select login_name from business_supplier_users_manage where login_name = '" + loginName + "'";
		String transportSql = "select login_id from transport_unit_management where login_id = '" + loginName + "'";
		List<Map<String,Object>> customerList = jdbc.queryForList(customerSql);
		List<Map<String,Object>> supplierList = jdbc.queryForList(supplierSql);
		List<Map<String,Object>> transportList = jdbc.queryForList(transportSql);
		if(customerList.size() == 0 && supplierList.size() == 0  && transportList.size() == 0) {
		SupplierUser suppliers = new SupplierUser();
		suppliers.setSupplier_code(code);
		suppliers.setSupplier(supplier);
		suppliers.setLogin_name(loginName);
		suppliers.setSupplier_name(supplierName);
		suppliers.setLogin_password(MD5Utils.md5(loginPassword));
		suppliers.setPhone(phone);
		suppliers.setAffiliated_organization(organization);
		suppliers.setType(type);
		suppliers.setTives(effective);
		suppliers.setExplains(explain);
		suppliers = dao.saveAndFlush(suppliers);
		if (suppliers != null)
			return suppliers;
		else
			return null;
		}else {
			return null;
		}
	}

}
